/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 *  The greatest achievement is selflessness. The greatest worth is self-mastery.
 *  The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 *  The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 *  The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 *  The greatest goodness is a peaceful mind. The greatest patience is humility.
 *  The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 *  The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 * 
 * @author Ashin Wimalajeewa (ash)
 */
package org.javaongems.win.client;

import java.util.Iterator;

import org.javaongems.std.client.ListenableStackPanel;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;

public class OutlookBar extends Composite {
	static public interface Listener {
		public void onCategoryOpen(int idx, OutlookBarList list);
		public void onCategoryClose(int idx, OutlookBarList list);
		public void onItemSelected(int idx, OutlookBarItem sender);
		public void onItemHoverEnter(int idx, OutlookBarItem sender);		
		public void onItemHoverLeave(int idx, OutlookBarItem sender);				
	}
	
	static public class ListenerAdapter implements Listener {
		public void onCategoryClose(int idx, OutlookBarList list) {}
		public void onCategoryOpen(int idx, OutlookBarList list) {}
		public void onItemHoverEnter(int idx, OutlookBarItem sender) {}
		public void onItemHoverLeave(int idx, OutlookBarItem sender) {}
		public void onItemSelected(int idx, OutlookBarItem sender) {}
	}
	
	final static private int stackLabelSz = 32;	// 20
	private ListenableStackPanel container;
	private StackHandler stackHandler = new StackHandler();
	private Listener listener;
	
	public OutlookBar() {
		container = new ListenableStackPanel();
		initWidget(container);
		UIObject.setStyleName(getElement(), "winforms-OutlookBar", true);
		container.setListener(stackHandler);
	}
	
//	private int heightInPixels;
//	
//	public void setHeight(String height) {
//		String val = height.substring(0, height.length()-2);
//		heightInPixels = Integer.parseInt(val);
//		super.setHeight(height);
//	}
	
	public void setPixelSize(int width, int height) {
		super.setPixelSize(width, height);
		refresh();
	}

	public void setListener(Listener listener) {
		this.listener = listener;
	}
	
	public OutlookBarList add(String name, boolean asHtml) {
		OutlookBarList ret = new OutlookBarList();
		container.add(ret, name, asHtml);
		ret.setOwner(this);
		refresh();
		return ret;
	}
	
	public boolean remove(OutlookBarList w) {
		boolean ret = container.remove(w);
		if (ret) {
			w.setOwner(null);
			refresh();
		}
		return ret;
	}
	
	public boolean remove(int idx) {
		OutlookBarList w = getCategoryList(idx);
		boolean ret = container.remove(idx);
		if (ret)
			w.setOwner(null);
		return ret;
	}
	
	public Iterator iterator() {
		return container.iterator();
	}
	
	public void clear() {
		int count = size();
		for (int i = 0; i < count; i++) 
			remove(0);
	}
	
	public int size() {
		return container.getWidgetCount();
	}
	
	public OutlookBarList getCategoryList(int idx) {
		return (OutlookBarList) container.getWidget(idx);
	}
	
	public void selectCategory(int catIdx) {
		container.showStack(catIdx);
	}
	
	public int getSelectedCategory() {
		return container.getStackIndex();
	}
	
	public void refresh() {
		int idx = container.getStackIndex();
		if (idx < 0)
			return;
		int sz = calcNewScrollPanelHeight();
		OutlookBarList w = (OutlookBarList) container.getWidget(idx);
		w.resizeHeight(sz);
	}
	
	protected void onItemSelected(int idx, OutlookBarItem itm) {
		if (listener != null)
			listener.onItemSelected(idx, itm);
	}
	
	protected void onItemHoverEnter(int idx, OutlookBarItem itm) {
		if (listener != null)
			listener.onItemHoverEnter(idx, itm);
	}

	protected void onItemHoverLeave(int idx, OutlookBarItem itm) {
		if (listener != null)
			listener.onItemHoverLeave(idx, itm);
	}
	
	private int calcNewScrollPanelHeight() {
		int ret = getOffsetHeight();
		int count = container.getWidgetCount();
		if (count > 0) {
			int diff = stackLabelSz * count;
			ret = ret - diff;
		}
		return ret;
	}
	
	private class StackHandler implements ListenableStackPanel.Listener {
		public void onStackClose(int idx, Widget target) {
			if (listener != null) {
				OutlookBarList list = (OutlookBarList) target;
				listener.onCategoryClose(idx, list);
			}
		}

		public void onStackOpen(int idx, Widget target) {
			if (listener != null) {
				OutlookBarList list = (OutlookBarList) target;
				listener.onCategoryOpen(idx, list);
			}
		}
	}
}
